package com.xq.tmall.agu.ferign;


import com.xq.tmall.entity.WebPage;
import com.xq.tmall.util.JsoupUitl;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.springframework.beans.factory.annotation.Value;

import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

/**
 * 抽象爬虫
 *
 * @author chenguoxiang
 * @create 2019-03-07 9:25
 **/
@Slf4j
public abstract class AbstractFetchStockSpider<T>{
    //任务执行容器
    protected BlockingQueue<T> queue;
    protected Map<String, String> cookie;
    protected String referer;
    protected String pageUrl;
    protected Integer timeOut;
    @Value("${spider.useagent}")
    protected String useAgent;

    //Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
    protected Proxy proxy;
    protected Boolean ignoreContentType;
    protected Boolean ignoreHttpErrors;
    protected Connection.Method method;

    /**
     * 执行页面抓取和解析
     */
    public List<T> fetchData() {
        List<T> results = new ArrayList<>();
        try {
            if (proxy == null) {
                throw new IllegalStateException("Proxy not set");
            }

            WebPage webPage = JsoupUitl.getWebPage(
                    pageUrl, method,
                    timeOut, useAgent, referer,
                    cookie, proxy,
                    ignoreContentType, ignoreHttpErrors);

            if (webPage == null || webPage.getDocument() == null) {
                throw new RuntimeException("Failed to fetch page: " + pageUrl);
            }

            results = parsePage(webPage);
        } catch (Exception ex) {
            log.error("Page processing failed: {}", pageUrl, ex);
        }
        return results;
    }

    /**
     * 解析页面内容
     */
    protected abstract List<T> parsePage(WebPage webPage) throws Exception;
    public BlockingQueue<T> getQueue() {
        return queue;
    }

    public void setQueue(BlockingQueue<T> queue) {
        this.queue = queue;
    }


    public Map<String, String> getCookie() {
        return cookie;
    }

    public void setCookie(Map<String, String> cookie) {
        this.cookie = cookie;
    }

    public String getReferer() {
        return referer;
    }

    public void setReferer(String referer) {
        this.referer = referer;
    }

    public String getPageUrl() {
        return pageUrl;
    }

    public void setPageUrl(String pageUrl) {
        this.pageUrl = pageUrl;
    }

    public Integer getTimeOut() {
        return timeOut;
    }

    public void setTimeOut(Integer timeOut) {
        this.timeOut = timeOut;
    }

    public String getUseAgent() {
        return useAgent;
    }

    public void setUseAgent(String useAgent) {
        this.useAgent = useAgent;
    }

    public Proxy getProxy() {
        return proxy;
    }

    public void setProxy(Proxy proxy) {
        this.proxy = proxy;
    }

    public Boolean getIgnoreContentType() {
        return ignoreContentType;
    }

    public void setIgnoreContentType(Boolean ignoreContentType) {
        this.ignoreContentType = ignoreContentType;
    }

    public Boolean getIgnoreHttpErrors() {
        return ignoreHttpErrors;
    }

    public void setIgnoreHttpErrors(Boolean ignoreHttpErrors) {
        this.ignoreHttpErrors = ignoreHttpErrors;
    }

    public Connection.Method getMethod() {
        return method;
    }

    public void setMethod(Connection.Method method) {
        this.method = method;
    }
}
